package com.class04.service.impl;

import com.class04.mapper.ScheduleMapper;
import com.class04.pojo.Schedule;
import com.class04.service.ScheduleService;
import com.class04.util.PageBean;
import com.class04.util.R;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassDescription:
 * @JdkVersion: 17
 * @Author: yxy
 * @Created: 2023/12/22 15:04
 */
@Service
public class ScheduleServiceImpl implements ScheduleService {

    @Autowired
    private ScheduleMapper scheduleMapper;

    @Override
    public R page(int pageSize, int currentPage) {
        //分页
        PageHelper.startPage(pageSize, currentPage);
        // sql limit x ,y;
//        查询
        List<Schedule> schedules = scheduleMapper.queryList();
        //分页数据装配
        PageInfo<Schedule> info = new PageInfo<>(schedules);
        //装配PageBean
        PageBean<Schedule> pageBean = new PageBean<>();
        pageBean.setCurrentPage(currentPage);
        pageBean.setPageSize(pageSize);
        pageBean.setTotal(info.getTotal());
        pageBean.setData(info.getList());
        R ok = R.ok(pageBean);
        return ok;
    }

    @Override
    public R remove(Integer id) {
        int row = scheduleMapper.deleteById(id);
        if (row > 0) {
            return R.ok(null);
        }
        return R.fail(null);
    }

    @Override
    public R save(Schedule schedule) {
        int row = scheduleMapper.insertSchedule(schedule);
        if (row > 0) {
            return R.ok(null);
        }
        return R.fail(null);
    }

    @Override
    public R update(Schedule schedule) {
//        判断id不能为空
        if (schedule.getId() == null || "".equals(schedule.getId())) {
            return R.fail("核心参数id没传，无法修改");
        }
        int row = scheduleMapper.updateSchedule(schedule);
        if (row > 0) {
            return R.ok(null);
        }
        return R.fail(null);
    }
}
